<?php
namespace Home\Controller;
use Think\Controller;
/*
  type=0 感染性
  type=1 损伤性
  type=3 化学性
  type=4 药物性
  type=5 病理性 
*/
class BoxController extends ZMController {
    //转运箱状态列表
    public function ListCurr(){
        $keyword =I('keyword');
		$keyword=snocompletion($keyword);
        $Model = M();
		$region=session('user.region');
		$where="where 1=1";
		if($region!=999){
			$where.=" and region=$region";
		}
		$arr=$Model->db(1,'DB_CONFIG1')->query("select hid from ddwk_hospital $where and hid<>11");
		foreach($arr as $re){
			$hid.=','.$re['hid'];
		}
		$hid=ltrim($hid,',');
		//30天只内
        $startdate=date('Y-m-d',strtotime("-30 day"));
		$enddate=date('Y-m-d');
		$resCollect=$Model->db(1,'DB_CONFIG1')->query("select a.sno,a.type,b.ctime,b.pdate,b.areaid,b.custid,c.name as custname,d.name from ddwk_collect_item as a left join ddwk_collect as b on a.cid=b.cid left join ddwk_hcustomer as c on b.custid=c.id left join ddwk_hospital as d on b.hid=d.hid where a.sno like '%$keyword' and b.pdate>='$startdate' and b.pdate<='$enddate' and d.hid in ($hid) order by b.ctime desc");
		$resUnload=$Model->db(1,'DB_CONFIG1')->query("select a.sno,a.type,b.ctime,b.pdate,c.name from ddwk_unload_item as a left join ddwk_unload as b on a.uid=b.uid left join ddwk_hospital as c on b.hid=c.hid where a.sno like '%$keyword' and b.pdate>='$startdate' and b.pdate<='$enddate' and c.hid in ($hid) order by b.ctime desc");
		$arr=array();
		foreach($resCollect as &$row1){
			$row1['status']='0';//收集
			$areaid=$row1['areaid'];
			if(!empty($areaid)){//科室
				$ar='';
			    $area=explode(',',$areaid);
			    foreach($area as $r){
				    $res=$Model->db(1,'DB_CONFIG1')->query("select areaname from ddwk_area where id=$r");
				    $ar.=$res['0']['areaname'].',';
			    }
				$ar=rtrim($ar,',');
				$row1['area']=$ar;
			}
			foreach($resUnload as $key1=>&$row2){
				$row2['status']='1';//入库
				if($row1['pdate']<$row2['pdate']){
					$arr[]=$row2;
					unset($resUnload[$key1]);
				}				
			}
			$arr[]=$row1;
		}
		if(!empty($resUnload)){
			foreach($resUnload as $row3){
				$row3['status']='1';//入库
				$arr[]=$row3;
			}
		}
		$companyCollect=$this->companyCollect($keyword,$startdate,$enddate);
		if(!is_numeric($companyCollect)&&!empty($companyCollect)){
			$result=array();
			foreach($arr as $row3){
				foreach($companyCollect as $key2=>&$row4){
					$row4['status']='2';//处置单位收集
					if($row3['pdate']<$row4['pdate']){
						$result[]=$row4;
						unset($companyCollect[$key2]);
					}				
				}
				$result[]=$row3;
			}
			if(!empty($companyCollect)){
				foreach($companyCollect as $row5){
					$result[]=$row5;
				}
			}
			$arr=$result;
		}
		$this->ajax($arr);
    }
    private function companyCollect($keyword,$startdate,$enddate){//处置单位收集
		try{
		    $Model=M();
			if(empty($keyword)||empty($startdate)||empty($enddate)){
				E('参数出错',10001);
			}
		    $res=$Model->db(2,'DB_CONFIG2')->query("select a.sno,a.type,b.pdate,b.ctime,c.name from ddwk_collect_item as a left join ddwk_collect as b on a.cid=b.cid left join ddwk_customer as c on b.custid=c.id where a.sno like '%$keyword' and b.pdate>='$startdate' and b.pdate<='$enddate' order by b.ctime desc");
			foreach($res as &$row){
				switch($row['type']){
					case 30:
					    $row['type']='0';
						break;
					case 31:
					    $row['type']='1';
						break;
					case 32:
					    $row['type']='4';
						break;
					case 33:
					    $row['type']='5';
						break;
					case 34:
					    $row['type']='0';
						break;	
				}
			}
			return $res;
		}
		catch(\Exception $e){
			return $e->getCode();//抛出错误代码
		}
	}
	public function smallCurr(){//袋子追溯
		$keyword=I('keyword');
		if(strlen($keyword)!=9&&strlen($keyword)!=11){//条码不完整
			exit(0);
		}
		$Model=M()->db(1,'DB_CONFIG1');
		$res=$Model->query("select a.time,a.areaid,b.sno,c.pdate,c.hid,b.type,d.name as ename,e.name as sname,f.name as custname from ddwk_collect_small as a left join ddwk_collect_item as b on a.iid=b.ciid left join ddwk_collect as c on a.cid=c.cid left join ddwk_employee as d on c.empid=d.id left join ddwk_employee as e on c.sid=e.id left join ddwk_hcustomer as f on c.custid=f.id where a.code='$keyword'");//相关信息查询
		if(empty($res)){
			$res=array();
			$this->ajax($res);
		}
		foreach($res as &$row){
			$hid=$row['hid'];
			$hosp=$Model->table("ddwk_hospital")->where("hid=$hid")->find();
			$row['name']=$hosp['name'];
			$row['code']=$keyword;
			if(strlen($keyword)==9){//以前的条码
			    $code=substr($keyword,0,5);
				$areaid=(int)$code;
			}
			else if(strlen($keyword)==11){//现在的随机条码
			    $areaid=$row['areaid'];
			}
			$area=$Model->query("select areaname from ddwk_area where id=$areaid");//查询所属科室
			$row['areaname']=$area['0']['areaname'];
		}
		$this->ajax($res);
	}
}